
public class Interpreter {
	
	String[] commandStrings;
	double[] variables = new double[100];
	String[] varNames = new String[100];
	int activeVariables = 0;
	
	
	public Interpreter(){
		
	}
	
	public Interpreter(String text){
		interprete(text);
	}
	
	
	public void interprete(String text){
		commandStrings = text.split("\n");
		for (int i = 0; i < commandStrings.length; i++) {
			interpreteLine(commandStrings[i], i);
		}
	}
	
	public void interprete(String text, int start, int end){
		commandStrings = text.split("\n");
		for (int i = start; i <= end; i++) {
			interpreteLine(commandStrings[i], i);
		}
	}
	
	public void interpreteLoop(int start){
		for (int i = start; i <= commandStrings.length; i++) {
			if(!commandStrings[i].startsWith("endloop")){
				interpreteLine(commandStrings[i], i);
			} else {
				break;
			}
		}
	}

	private void interpreteLine(String commandLineTmp, int counter) {
		String commandLine = commandLineTmp;
		if(commandLine.startsWith("forward ")){
			commandLine = commandLine.replaceAll("forward", "");
			commandLine = commandLine.replaceAll(" ", "");
			MyLogo.logoWindow.forward(testVars(commandLine));
			
		}
		else if(commandLine.startsWith("rotateRight ")){
			commandLine = commandLine.replaceAll("rotateRight", "");
			commandLine = commandLine.replaceAll(" ", "");
			MyLogo.logoWindow.rotateRight(testVars(commandLine));
		}
		else if(commandLine.startsWith("rotateLeft ")){
			commandLine = commandLine.replaceAll("rotateLeft", "");
			commandLine = commandLine.replaceAll(" ", "");
			MyLogo.logoWindow.rotateLeft(testVars(commandLine));
		}
		else if(commandLine.startsWith("int ")){
			commandLine = commandLine.replaceAll("int", "");
			commandLine = commandLine.replaceAll(" ", "");
			varNames[activeVariables] = commandLine;
			activeVariables++;
		}
		else if(commandLine.startsWith("double ")){
			commandLine = commandLine.replaceAll("double", "");
			commandLine = commandLine.replaceAll(" ", "");
			varNames[activeVariables] = commandLine;
			activeVariables++;
		}
		else if(commandLine.startsWith("print ")){
			commandLine = commandLine.replaceAll("print", "");
			commandLine = commandLine.replaceAll(" ", "");
			System.out.println(testVars(commandLine));
		}
		else if(commandLine.startsWith("loop ")){
			commandLine = commandLine.replaceAll("loop", "");
			commandLine = commandLine.replaceAll(" ", "");
			for(int i = 1; i < testVars(commandLine); i++){
				interpreteLoop(counter+1);
			}
		}
		else{
			for (int i = 0; i < activeVariables; i++) {
				if(commandLine.startsWith(varNames[i])){
					commandLine = commandLine.replaceAll(varNames[i], "");
					commandLine = commandLine.replaceAll("=", "");
					commandLine = commandLine.replaceAll(" ", "");
					variables[i] = Double.parseDouble(commandLine);
				}
			}
			
			
			
		}
	}
	
	private double testVars(String stringToTest){
		String commandLine = stringToTest;
		boolean variableReconised = false;
		double dToRet = 0;
		for (int i = 0; i < activeVariables; i++) {
			if(varNames[i].equals(commandLine)){
				variableReconised = true;
				dToRet = variables[i];					
			}
		}
		if(!variableReconised){
			return Double.parseDouble(commandLine);
		} else {
			return dToRet;
		}
	}
	
}
